/**
 * 
 */
package com.xbang.attendance.web.action.report.regionsales;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.DeliveryDetails;
import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.helper.order.DeliveryOrderReport;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.BaseActionSupport;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 2886204759816132683L;

	@Autowired
	private SystemService systemService;

	private List<DeliveryOrderReport> deliveryOrderReports = new ArrayList<DeliveryOrderReport>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	@Action("/report/regionsales/list")
	public String list() throws Exception {

		String groupHQLSentence = "SELECT detail.deliveryOrder.region.id ,detail.product.id,detail.product.productCode,detail.product.productName, SUM(detail.productNumber) FROM DeliveryDetails detail WHERE 1 = 1 ";

		SearchComponent tmpSearchComponent = new SearchComponent();
		tmpSearchComponent.getParameters().putAll(searchComponent.getParameters());
		tmpSearchComponent.getWhereSentences().putAll(searchComponent.getWhereSentences());
		tmpSearchComponent.addSearchElement("groupby.detail.deliveryOrder.region.id", null);
		tmpSearchComponent.addSearchElement("groupby.detail.product.id", null);

		Result tmpResult = systemService.searchByPage(groupHQLSentence, tmpSearchComponent, page);
		// /////
		List<Object[]> lists = tmpResult.getItems();
		page = tmpResult.getPage();

		if (lists == null || lists.isEmpty()) {
			return "index";
		}

		Set<Integer> ids = new HashSet<Integer>();
		for (Object[] objects : lists) {
			ids.add((Integer) objects[0]);
		}

		String hqlSentence = "FROM DeliveryDetails deliverydetails WHERE 1 = 1";
		SearchComponent tmpSearchComponent1 = new SearchComponent();
		tmpSearchComponent1.getParameters().putAll(searchComponent.getParameters());
		tmpSearchComponent1.getWhereSentences().putAll(searchComponent.getWhereSentences());
		tmpSearchComponent1.addSearchElement("in.deliverydetails.deliveryOrder.region.id", ids);

		List<DeliveryDetails> deliverydetails = systemService.searchForList(hqlSentence, tmpSearchComponent1);

		if (deliverydetails != null && !deliverydetails.isEmpty()) {
			for (Object[] objects : lists) {
				DeliveryOrderReport deliveryOrderReport = new DeliveryOrderReport();
				for (DeliveryDetails deliverydetail : deliverydetails) {
					Integer regionId = (Integer) objects[0];
					Integer productId = (Integer) objects[1];
					if (regionId.intValue() == deliverydetail.getDeliveryOrder().getRegion().getId().intValue()
							&& productId == deliverydetail.getProduct().getId().intValue()) {
						deliveryOrderReports.add(deliveryOrderReport.copyPerperties(deliverydetail.getDeliveryOrder().getWarehouse(), deliverydetail
								.getDeliveryOrder().getRegion(), deliverydetail.getDeliveryOrder().getMaintenanceStation(), new Product().copyProperties(
								(Integer) objects[1], objects[3].toString(), objects[2].toString()), (BigDecimal) objects[4], false, false, true));
						break;
					}
				}
			}
		}

		return "index";
	}

	@Override
	public void resetCustomerSearchCriteria() {

	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public List<DeliveryOrderReport> getDeliveryOrderReports() {
		return deliveryOrderReports;
	}

	public void setDeliveryOrderReports(List<DeliveryOrderReport> deliveryOrderReports) {
		this.deliveryOrderReports = deliveryOrderReports;
	}
}
